using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using Common;
using Common.Enum;

namespace SProject
{
	[Serializable]
	public class ProjectManage
	{
		private static ProjectManage _ProjectManage;

		public ObservableCollection<Project> AllProjectList { get; private set; }

		public static ProjectManage GetInstance()
		{
			if (_ProjectManage == null)
			{
				_ProjectManage = new ProjectManage();
				_ProjectManage.InitProjectList();
			}
			return _ProjectManage;
		}

		private ProjectManage()
		{
		}

		public void InitProjectList()
		{
			AllProjectList = new ObservableCollection<Project>();
			string projectPath = Utility.GetProjectPath();
			string[] files = Directory.GetFiles(projectPath, "*" + Project.ProjectFileDotExt, SearchOption.TopDirectoryOnly);
			string[] array = files;
			foreach (string path in array)
			{
				AddProject(Project.Load(path));
			}
		}

		public ObservableCollection<Project> GetProjectByType(EProjectType type)
		{
			IEnumerable<Project> collection = AllProjectList.Where((Project prj) => prj.BasicInfo.Type == type);
			return new ObservableCollection<Project>(collection);
		}

		public string GetNewProjectName()
		{
			string text = "project";
			for (int i = 1; i < int.MaxValue; i++)
			{
				string name = text + i;
				IEnumerable<Project> source = AllProjectList.Where((Project prj) => prj.BasicInfo.Name.Equals(name));
				if (source.Count() == 0)
				{
					return name;
				}
			}
			return text + " 1";
		}

		public bool IsExistProjectName(string name)
		{
			IEnumerable<Project> source = AllProjectList.Where((Project prj) => prj.BasicInfo.Name.Equals(name));
			if (source.Count() == 0)
			{
				return false;
			}
			return true;
		}

		public Project GetProject(string name)
		{
			IEnumerable<Project> source = AllProjectList.Where((Project prj) => prj.BasicInfo.Name.Equals(name));
			if (source.Count() == 1)
			{
				return source.ToList()[0];
			}
			return null;
		}

		public Project CreatePasteProject(Project project)
		{
			string value = "- copy";
			Project project2 = new Project(project);
			StringBuilder stringBuilder = new StringBuilder(project2.BasicInfo.Name);
			int num = 0;
			while (IsExistProjectName(stringBuilder.ToString()))
			{
				num++;
				if (num == 1)
				{
					stringBuilder.Append(value);
				}
				else if (num > 1)
				{
					stringBuilder.Clear();
					stringBuilder.Append(project2.BasicInfo.Name).Append(value).Append("(")
						.Append(num)
						.Append(")");
				}
				if (stringBuilder.Length > 255)
				{
					stringBuilder.Remove(200, stringBuilder.Length - 255);
				}
			}
			project2.BasicInfo.Name = stringBuilder.ToString();
			project2.Save();
			return project2;
		}

		public bool AddProject(Project project)
		{
			if (project == null)
			{
				return false;
			}
			AllProjectList.Add(project);
			return true;
		}

		public bool DeleteProject(Project project)
		{
			if (project != null && AllProjectList.Contains(project))
			{
				project.DeleteFile();
				return AllProjectList.Remove(project);
			}
			return false;
		}

		public bool ContainsProject(Project project)
		{
			if (project == null)
			{
				return false;
			}
			return AllProjectList.Contains(project);
		}
	}
}
